package ar.uba.fi.posgrado.academy.web.action.course;

import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.collections.CollectionUtils;
import org.apache.log4j.Logger;
import org.apache.struts.action.Action;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;

import ar.uba.fi.posgrado.academy.model.Course;
import ar.uba.fi.posgrado.academy.service.CourseService;
import ar.uba.fi.posgrado.academy.web.form.course.EnrollmentForm;
import ar.uba.fi.posgrado.economy.web.util.CourseToLabelValueTransformer;

/**
 * Muestra la pantalla para inscribir {@link Student alumno} en un {@link Course curso}
 * 
 * @author ariel.piechotka
 *
 */
public class ShowEnrollStudentAction extends Action {
	/** Logger */
	private static Logger logger = Logger.getLogger(ShowEnrollStudentAction.class);

	private CourseService courseService;

	public void setCourseService(CourseService courseService) {
		this.courseService = courseService;
	}

	/**
	 * @see org.apache.struts.action.Action#execute(org.apache.struts.action.ActionMapping,
	 *      org.apache.struts.action.ActionForm,
	 *      javax.servlet.http.HttpServletRequest,
	 *      javax.servlet.http.HttpServletResponse)
	 */
	@SuppressWarnings("unchecked")
	@Override
	public ActionForward execute(ActionMapping mapping, ActionForm actionForm,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		if (logger.isDebugEnabled())
			logger.debug("Por mostrar la pantalla de inscripcion en curso...");


		EnrollmentForm form = (EnrollmentForm) actionForm;
		form.clear();

		List allCourses = courseService.getAll();
		CollectionUtils.transform(allCourses, new CourseToLabelValueTransformer());
		form.setCourseOptions(allCourses);

		logger.info("Mostrar pantalla inscripcion en curso finalizado");
		return mapping.findForward("success");
	}
}
